package org.ushio.nchu_22201404.dao;

import org.apache.ibatis.annotations.*;
import org.ushio.common_module.entity.RepairRecord;

import java.util.List;

@Mapper
public interface MaintainRecordDao {
    @Insert("INSERT INTO repair_record (Appointment_Date, Repair_Type, Issue_Details, Rating, Complaint, Issue_Resolved, Phone_Number, Receipt_Status, Repairman_ID)" +
            "VALUES (#{appointmentDate}, #{repairType}, #{issueDetails}, 0, 0, 0, #{phoneNumber}, 0, 1)")
    @Options(useGeneratedKeys = true, keyProperty = "repairRecordID")
    void insertMaintainRecord(RepairRecord repairRecord);

    @Update("UPDATE repair_record " +
            "SET Rating = #{rating}, Issue_Resolved = #{issueResolved} " +
            "WHERE Phone_Number = #{phoneNumber}")
    void maintainSetIssueResolved(RepairRecord repairRecord);

    @Select("SELECT Issue_Resolved " +
            "FROM repair_record " +
            "WHERE Phone_Number = #{phoneNumber}")
    @Results({
            @Result(property = "issueResolved", column = "Issue_Resolved")
    })
    RepairRecord maintainGetIssueResolved(@Param("phoneNumber") String phoneNumber);

    @Update("UPDATE repair_record " +
            "SET Receipt_Status = 1 " +
            "WHERE Phone_Number = #{phoneNumber}")
    void maintainSetIsSigned(RepairRecord repairRecord);

    @Select("SELECT Receipt_Status " +
            "FROM repair_record " +
            "WHERE Phone_Number = #{phoneNumber}")
    @Results({
            @Result(property = "receiptStatus", column = "Receipt_Status")
    })
    RepairRecord maintainGetIsSigned(@Param("phoneNumber") String phoneNumber);

    @Select("SELECT Appointment_Date, Repair_Type, Issue_Details, Phone_Number " +
            "FROM repair_record\n"+
            "WHERE Receipt_Status = 0")
    @Results({
            @Result(property = "appointmentDate", column = "Appointment_Date"),
            @Result(property = "repairType", column = "Repair_Type"),
            @Result(property = "issueDetails", column = "Issue_Details"),
            @Result(property = "phoneNumber", column = "Phone_Number"),
            @Result(property = "receiptStatus", column = "Receipt_Status")
    })
    List<RepairRecord> selectMaintainRecordToCustomerService();

    @Select("SELECT * FROM repair_record")
    @Results({
            @Result(property = "repairRecordID", column = "Repair_Record_ID"),
            @Result(property = "appointmentDate", column = "Appointment_Date"),
            @Result(property = "issueDetails", column = "Issue_Details"),
            @Result(property = "repairType", column = "Repair_Type"),
            @Result(property = "rating", column = "Rating"),
            @Result(property = "complaint", column = "Complaint"),
            @Result(property = "receiptStatus", column = "Receipt_Status"),
            @Result(property = "issueResolved", column = "Issue_Resolved"),
            @Result(property = "applicationTime", column = "ApplicationTime"),
            @Result(property = "repairmanID", column = "Repairman_ID"),
            @Result(property = "phoneNumber", column = "Phone_Number")
    })
    List<RepairRecord> selectMaintainRecordToAdmin();

    @Select("SELECT * FROM repair_record WHERE Phone_Number = #{phoneNumber}")
    @Results({
            @Result(property = "repairRecordID", column = "Repair_Record_ID"),
            @Result(property = "appointmentDate", column = "Appointment_Date"),
            @Result(property = "issueDetails", column = "Issue_Details"),
            @Result(property = "repairType", column = "Repair_Type"),
            @Result(property = "rating", column = "Rating"),
            @Result(property = "complaint", column = "Complaint"),
            @Result(property = "receiptStatus", column = "Receipt_Status"),
            @Result(property = "issueResolved", column = "Issue_Resolved"),
            @Result(property = "applicationTime", column = "ApplicationTime"),
            @Result(property = "repairmanID", column = "Repairman_ID"),
            @Result(property = "phoneNumber", column = "Phone_Number")
    })
    List<RepairRecord> selectByPhoneNumber(@Param("phoneNumber") String phoneNumber);
}
